package com.yoomiito.app.ui.search.youpin

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import cn.droidlover.xdroidmvp.cache.SharedPref
import cn.droidlover.xdroidmvp.router.Router
import com.yoomiito.app.Constant
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.base.BaseActivity
import com.yoomiito.app.model.home.HotSearch
import com.yoomiito.app.model.home.TaoBaoHotSearch
import com.yoomiito.app.model.home.YouPinHotSearch
import com.yoomiito.app.ui.search.SearchResultActivity
import com.yoomiito.app.utils.Const
import com.yoomiito.app.utils.L
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import kotlinx.android.synthetic.main.act_search.historySearchFlowLayout
import kotlinx.android.synthetic.main.act_search.hotSearch
import kotlinx.android.synthetic.main.act_search.hotSearchFlowLayout
import kotlinx.android.synthetic.main.act_youpin_search.*
import kotlinx.android.synthetic.main.item_search.*
import kotlinx.android.synthetic.main.item_search_toolbar.*
import java.util.ArrayList

class YouPinSearchActivity :BaseActivity<YouPinSearchPresenter>(){
    private var searchList: MutableList<String>? = null
    private var tagAdapter: TagAdapter<String>? = null
    private var key: String = ""
    private var hotSearchData:List<String>?=null
    private val defaultSearchHint = "输入优品商品名"
    private var mConfigKeyWord = ""
    companion object{
        fun start(context: Context){
            val  intent = Intent(context,YouPinSearchActivity::class.java)
            context.startActivity(intent)
        }
    }

    override fun getLayoutId(): Int = R.layout.act_youpin_search

    override fun newP(): YouPinSearchPresenter = YouPinSearchPresenter(App.mAppConfig)

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        act_search_back.setOnClickListener { finish() }
        item_search_et.hint = defaultSearchHint
        act_search_search.setOnClickListener {
            key = item_search_et.text.toString().trim { it <= ' ' }
            if (TextUtils.isEmpty(key)) {
                val hint = item_search_et.hint.toString()
                if (defaultSearchHint==hint){
                    showToast("请输入搜索内容")
                    return@setOnClickListener
                }else{
                    key = hint
                }
            }
            putCache(key)
            toResult()
        }
        delSearchHistory.setOnClickListener {
            SharedPref.getInstance(App.getInstance()).remove(Const.Common.YouSearchHistory)
            searchList?.clear()
            tagAdapter?.notifyDataChanged()
            searchHistoryView.visibility = View.GONE
        }
        item_search_del.setOnClickListener {
            item_search_et.setText("")
        }
    }

    override fun onResume() {
        super.onResume()
        p?.getHotSearch()
        searchList = SharedPref.getInstance(App.getInstance()).getListData(Const.Common.YouSearchHistory, String::class.java)
        if (searchList == null) {
            searchList = ArrayList()
        }
        searchHistoryView.visibility = if (searchList?.size==0) View.GONE else View.VISIBLE
        tagAdapter = object : TagAdapter<String>(searchList){
            override fun getView(parent: FlowLayout?, position: Int, t: String?): View {
                val tv = LayoutInflater.from(this@YouPinSearchActivity).inflate(R.layout.item_tv, parent, false) as TextView
                tv.text = t
                return tv
            }

        }
        historySearchFlowLayout.adapter = tagAdapter
        historySearchFlowLayout.setOnTagClickListener { view: View?, position: Int, parent: FlowLayout? ->
            key = searchList!![position]
            item_search_et.setText(key)
            toResult()
            false
        }
        hotSearchFlowLayout.setOnTagClickListener { view: View?, position: Int, parent: FlowLayout? ->
            key = hotSearchData?.get(position)!!
            item_search_et.setText(key)
            putCache(key!!)
            toResult()
            false
        }
    }

    fun setHotSearch(data: YouPinHotSearch?){
        if (data!=null){
            if (data.search_title?.isNotEmpty()){
                item_search_et.hint = data.search_title
            }
            mConfigKeyWord = data.search_title
            if (data.fine_hot_search!=null){
                hotSearchData = data.fine_hot_search
                hotSearch.visibility = if(data.fine_hot_search.isEmpty()) View.GONE else View.VISIBLE
                hotSearchFlowLayout.adapter =  object : TagAdapter<String>(data.fine_hot_search){
                    override fun getView(parent: FlowLayout?, position: Int, t: String?): View {
                        val tv = LayoutInflater.from(this@YouPinSearchActivity).inflate(R.layout.item_tv, parent, false) as TextView
                        tv.text = t
                        return tv
                    }

                }
            }
        }
    }

    private fun toResult() {
        YouPinSearchResultActivity.start(this,key,mConfigKeyWord)
    }

    private fun putCache(str:String){
        if (!searchList?.contains(str)!!) {
            if (searchList?.size!! >= 5) {
                searchList?.removeAt((searchList?.size!!-1))
            }
            searchList?.add(0, str)
            SharedPref.getInstance(App.getInstance()).putListData(Const.Common.YouSearchHistory, searchList)
        }
    }

}